package main

import (
	"fmt"
	"net/http"
)

var (
	indexHtml = `<!DOCTYPE html>
<html lang="zh-ch">
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
		<title>%s</title>
	</head>

	<body>
	
    %s

	</body>

</html> `

	qrcodeHtml = `<img src="data:image/png;base64,%s" alt="评分页面二维码">
    <br>
    <input type="button" style="height: 40px; width: 110px;" value="展示评分结果" onclick="location.assign('http://%s:%s?key=%s')">
    `

	detailHtml = `<details open="">
    	<summary style="font-size: 18px;" >
        <table cellspacing="0" border="1" style="height: auto; width: 60%%; text-align: center;" >
          <tr align="center"  >
            <td style="font-weight: bold">%s</td>
            <td><img style="height: 100px; width: 100px;" src="%s"></img></td>
            <td>%s</td>
            <td>平均分：%v </td>
          <tr>
        </table>
      </summary>
    	<fieldset>
    		<legend>评分内容</legend>
          <table cellspacing="0" border="1" style="text-align: center;" >
            <tr align="center" style="font-weight: bold" ><td>序号</td><td>评价</td><td>评分</td><tr>
            %s
            <tr><td>总分</td><td>-</td><td>%v</td></tr>
          </table>
    	</fieldset>
    </details>`
	scoreTableHtml = `<tr><td>%v</td><td>%v</td><td>%v</td></tr>`
)

func index(w http.ResponseWriter, r *http.Request) {
	htmlSrc := ""
	if r.FormValue("key") == roundKey {
		// 展示评分内容
		for _, v := range memberList {
			t := ""
			idx := 1
			sum := 0
			for _, vv := range v.m {
				t += fmt.Sprintf(
					scoreTableHtml,
					idx,
					vv.note,
					vv.score,
				)
				idx += 1
				sum += vv.score
			}
			avg := 0
			if idx > 1 {
				avg = sum / (idx - 1)
			}
			htmlSrc += fmt.Sprintf(
				detailHtml,
				v.Name,
				v.ImagePath,
				v.Desc,
				avg,
				t,
				sum,
			)
		}
		htmlSrc = fmt.Sprintf(
			indexHtml,
			"评分结果",
			htmlSrc,
		)
	} else {
		// 只展示二维码 和 按钮
		htmlSrc = fmt.Sprintf(
			indexHtml,
			"开始评分",
			fmt.Sprintf(
				qrcodeHtml,
				outQrcodeBase64(fmt.Sprintf("http://%s:%s/score?key=%s", *ip, *port, roundKey)),
				*ip,
				*port,
				roundKey,
			),
		)
	}
	w.Write([]byte(htmlSrc))
}
